/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfata;

/**
 *
 * @author AlinT
 */
import Visitor.CalculVisitor;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JFrame;

public class Grafic extends JApplet implements Command {

   
    @Override
    /**
     * @see JApplet.init()
     */
  public void init() {
    setBackground(Color.white);
    setForeground(Color.white);
  }


    @Override
      /**
     * @see JApplet.update()
     */
  public void update(Graphics g)
	{
		super.update(g);


	}
    @Override
      /**
     * @see JApplet.paint()
     */
  public void paint(Graphics g) {

        repaint();
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.gray);
   
    g2.draw(new Line2D.Double(0, 250, 500, 250));
    g2.drawString("y", 255, 20);
    g2.drawString("0", 250, 250);
    g2.draw(new Line2D.Double(250, 0, 250, 500));
    g2.drawString("x", 470, 265);

    g2.setColor(Color.red);

    String functie= GUInterfata.getFunctie();
    CreazaCopac arb=new CreazaCopac(functie);

    ArrayList<Integer> X=new ArrayList<Integer>();
    ArrayList<Integer> Y=new ArrayList<Integer>();

    int x=0, y=0, pas=0;
    for(int i=0;i<501;i++)
        for(int j=245; j<255;j++)
        {
            if(i!=0){
                CalculVisitor c=new CalculVisitor(i-1);
                arb.arbore.acceptVisitor(c);
                y=(int)c.getValue();
                if(y==j) x=i;
            }else{
                CalculVisitor c=new CalculVisitor(0);
                arb.arbore.acceptVisitor(c);
                pas=(int)c.getValue();
            }
        }


    for(int i=-500; i<500;i++)
    {
        CalculVisitor c=new CalculVisitor(i);
        arb.arbore.acceptVisitor(c);
        double zz=c.getValue();
        long z1=Math.round(zz);
        int z=(int)z1;
        if(arb.parser.oke){ X.add(i+250-x); Y.add(500-z-pas); }
        else {X.add(i+250-x); Y.add(500-z-pas-250);}
    }

  for(int i=0; i<X.size()-1; i++)
      g2.draw(new Line2D.Double(X.get(i),Y.get(i),X.get(i+1),Y.get(i+1)));
  }

    @Override
      /**
     * @see Command.execute()
     */
  public void execute(){
    JFrame f = new JFrame("Grafic");
    JApplet applet = new Grafic();
    f.getContentPane().add("Center", applet);
    applet.init();
    f.pack();
   f.setSize(new Dimension(500, 500));
   f.show();
  }
}